package com.william.taskrobot.service;

import com.ansj.vec.domain.WordEntry;
import me.xiaosheng.util.Segment;
import me.xiaosheng.word2vec.Word2Vec;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * Word2VecServie class
 *
 * @author william
 * @date 2021/1/21
 */
public class Word2VecServie {
    public static Word2Vec vec;
    public static String path="D:/nlp/ansj/Google_word2vec_zhwiki1710_300d.bin";
    static {
        vec=new Word2Vec();
        try {
            vec.loadGoogleModel(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取相近词
     * @param word
     * @param count
     * @return
     */
    public static Set<WordEntry> getSimilarWords(String word, Integer count){
        return vec.getSimilarWords(word,count);
    }

    /**
     * 计算两个文本的相似度
     * @param src
     * @param dest
     * @return
     */
    public static float getSimilarity(String src,String dest){
        List<String> srcList= Segment.getWords(src);
        List<String> destList=Segment.getWords(dest);
         // vec.fastSentenceSimilarity(srcList,destList);  //快速计算相似度
       return vec.sentenceSimilarity(srcList,destList);
        //https://github.com/jsksxs360/Word2Vec
    }
}
